package com.zyg.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.alibaba.fastjson2.JSON;
import com.zyg.pojo.User;
import com.zyg.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
   @Autowired
   private UserService userService;

    @RequestMapping("/")
    public String login(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(User user, String code, HttpSession session){
        //判断验证码是否正确
        if(code!=null &&!code.equalsIgnoreCase(session.getAttribute("code").toString()))
        {
            return "redirect:/";
        }
      User u=  userService.login(user);
        if(u!=null){
            //保存用户id登录凭证
            StpUtil.login(u.getId());
            return "redirect:/toindex";
        }else{
            return "redirect:/";
        }
    }

    @RequestMapping("/code")
    @ResponseBody
    public String code(HttpSession session){
        session.removeAttribute("code");
        //调用hutool生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        String code = lineCaptcha.getCode();
        session.setAttribute("code",code);
        lineCaptcha.getImageBase64();
        //转换json 格式 fastjson2
        return JSON.toJSONString(lineCaptcha.getImageBase64());
    }

}
